package com.test1.administrator.website;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.test1.administrator.website.dao.DBOperator;
import com.test1.administrator.website.entity.Website;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2017/6/9.
 */

public class SendWebSite extends AppCompatActivity {
    private List<Map<String, Website>> mData;
    DBOperator dbOperator;
    MyAdapter send_TextView_Adapter;
    Set<Integer> send_Choice=new HashSet();
    int[] send_position;

    public List<Map<String,Website>> getData(){
        dbOperator=new DBOperator(SendWebSite.this);
        List<Map<String,Website>> list=dbOperator.query();
        return list;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_website);



        ListView listView=(ListView)findViewById(R.id.send_listview);
        FloatingActionButton send = (FloatingActionButton) findViewById(R.id.send);

        mData=getData();
        send_TextView_Adapter=new MyAdapter(this);
        listView.setAdapter(send_TextView_Adapter);


        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                获取send_id
                send_position=new int[send_Choice.size()];
                Iterator iterator=send_Choice.iterator();
                for(int j=0;j<send_Choice.size();j++){
                    send_position[j]=(int)iterator.next();
//                    Log.v("send_position"+j,"====================="+send_position[j]);
                }

                Intent send=new Intent();
                send.putExtra("send",send_position);
                setResult(MainActivity.SEND,send);

                finish();
            }
        });
//        onclick  返回id
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /**
             * Callback method to be invoked when an item in this AdapterView has
             * been clicked.
             * <p>
             * Implementers can call getItemAtPosition(position) if they need
             * to access the data associated with the selected item.
             *
             * @param parent   The AdapterView where the click happened.
             * @param view     The view within the AdapterView that was clicked (this
             *                 will be a view provided by the adapter)
             * @param position The position of the view in the adapter.
             * @param id       The row id of the item that was clicked.
             */
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                view.animate();
//                判断是否已选择，如果已选择从set中去除 如果没有选择 添加至set
                if(send_Choice.contains(position)){
                    send_Choice.remove(position);
                    Log.v("send_Choice","===============remove"+position);
                    Log.v("send_Choice","===============back"+view.getDrawingCacheBackgroundColor());
                    view.setBackgroundColor(0);
                }else{
                    send_Choice.add(position);
                    Log.v("send_Choice","===============add"+position);
                    view.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                }
            }
        });



    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * Dispatch onPause() to fragments.
     */
    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * Dispatch onResume() to fragments.  Note that for better inter-operation
     * with older versions of the platform, at the point of this call the
     * fragments attached to the activity are <em>not</em> resumed.  This means
     * that in some cases the previous state may still be saved, not allowing
     * fragment transactions that modify the state.  To correctly interact
     * with fragments in their proper state, you should instead override
     * {@link #onResumeFragments()}.
     */
    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     * Initialize the contents of the Activity's standard options menu.  You
     * should place your menu items in to <var>menu</var>.
     * <p>
     * <p>This is only called once, the first time the options menu is
     * displayed.  To update the menu every time it is displayed, see
     * {@link #onPrepareOptionsMenu}.
     * <p>
     * <p>The default implementation populates the menu with standard system
     * menu items.  These are placed in the {@link Menu#CATEGORY_SYSTEM} group so that
     * they will be correctly ordered with application-defined menu items.
     * Deriving classes should always call through to the base implementation.
     * <p>
     * <p>You can safely hold on to <var>menu</var> (and any items created
     * from it), making modifications to it as desired, until the next
     * time onCreateOptionsMenu() is called.
     * <p>
     * <p>When you add items to the menu, you can implement the Activity's
     * {@link #onOptionsItemSelected} method to handle them there.
     *
     * @param menu The options menu in which you place your items.
     * @return You must return true for the menu to be displayed;
     * if you return false it will not be shown.
     * @see #onPrepareOptionsMenu
     * @see #onOptionsItemSelected
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }


    public final class ViewHolder{
        public TextView send_textView;
    }


    public class MyAdapter extends BaseAdapter{

        private LayoutInflater mInflater;

        public MyAdapter(Context context) {
            this.mInflater = LayoutInflater.from(context);
        }

        public Website getItemText(int position){
            return mData.get(position).get(mData.get(position).keySet().iterator().next());
        }

        /**
         * Notifies the attached observers that the underlying data has been changed
         * and any View reflecting the data set should refresh itself.
         */
        @Override
        public void notifyDataSetChanged() {
            super.notifyDataSetChanged();
        }

        /**
         * How many items are in the data set represented by this Adapter.
         *
         * @return Count of items.
         */
        @Override
        public int getCount() {
            return mData.size();
        }

        /**
         * Get the data item associated with the specified position in the data set.
         *
         * @param position Position of the item whose data we want within the adapter's
         *                 data set.
         * @return The data at the specified position.
         */
        @Override
        public Object getItem(int position) {
            return null;
        }

        /**
         * Get the row id associated with the specified position in the list.
         *
         * @param position The position of the item within the adapter's data set whose row id we want.
         * @return The id of the item at the specified position.
         */
        @Override
        public long getItemId(int position) {
            return position;
        }

        /**
         * Get a View that displays the data at the specified position in the data set. You can either
         * create a View manually or inflate it from an XML layout file. When the View is inflated, the
         * parent View (GridView, ListView...) will apply default layout parameters unless you use
         * {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
         * to specify a root view and to prevent attachment to the root.
         *
         * @param position    The position of the item within the adapter's data set of the item whose view
         *                    we want.
         * @param convertView The old view to reuse, if possible. Note: You should check that this view
         *                    is non-null and of an appropriate type before using. If it is not possible to convert
         *                    this view to display the correct data, this method can create a new view.
         *                    Heterogeneous lists can specify their number of view types, so that this View is
         *                    always of the right type (see {@link #getViewTypeCount()} and
         *                    {@link #getItemViewType(int)}).
         * @param parent      The parent that this view will eventually be attached to
         * @return A View corresponding to the data at the specified position.
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if(convertView==null){
                holder=new ViewHolder();
                convertView=mInflater.inflate(R.layout.send_website_textview,null);
                holder.send_textView=(TextView)convertView.findViewById(R.id.send_textView);
                convertView.setTag(holder);
            }else{
                holder=(ViewHolder)convertView.getTag();
            }


//            取出list数据
            Set sData=mData.get(position).keySet();
            String id=(String)sData.iterator().next();
            Website website=mData.get(position).get(id);

            holder.send_textView.setText(website.getName()+"    "+website.getSite());
            return convertView;
        }
    }
}
